package com.example.demo.leetcode;
//字母异位词分组
//异位词指相同字母重新组合排列产生的新字符串
//
//返回这些子串的起始位置

import java.util.*;

public class Leetcode49 {

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String sorted = new String(chars);
            if (map.containsKey(sorted)) {
                map.get(sorted).add(str);
            } else {
                List<String> list = new ArrayList<>();
                list.add(str);
                map.put(sorted, list);
            }
        }
        return new ArrayList<>(map.values());
    }


    public static void main(String[] args) {

        System.out.println(new Leetcode49().groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
    }


}
